package com.csyf.manager.controller.system;

import com.csyf.manager.common.CommonConst;
import com.csyf.manager.entity.SysAgent;
import com.csyf.manager.entity.SysMerchant;
import com.csyf.manager.exception.ApiValidateException;
import com.csyf.manager.service.SysAgentService;
import com.csyf.manager.service.SysMerchBiz;
import com.csyf.manager.utils.ResultEntity;
import com.csyf.manager.utils.ResultUtli;
import com.csyf.manager.utils.page.JqGridVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * @Author: casper
 * @Description: 代理商
 * @Date: Created in 2019/3/20 16:58
 */
@Controller
@RequestMapping("/agent")
public class SysAgentController {

    @Autowired
    private SysAgentService sysAgentService;

    @Autowired
    private SysMerchBiz sysMerchService;

    /**
     * 代理商列表
     * @param vo
     * @param page
     * @param rows
     * @return
     */
    @PostMapping("/list")
    @ResponseBody
    public JqGridVo list(SysAgent vo, Integer page, Integer rows) {
        return sysAgentService.querydata(vo, page, rows);
    }

    /**
     * 新增代理商
     */
    @ResponseBody
    @PostMapping(value = "add")
    public ResultEntity add(SysAgent agentForm) {
        try {
            sysAgentService.saveAgent(agentForm);
            return ResultUtli.DEFAULT_SUCCESS("添加代理商成功", null);
        } catch (ApiValidateException e) {
            return ResultUtli.DEFAULT_ERROR("添加代理商失败" + e.getMessage());
        }
    }

    /**
     * 编辑用户详情
     * @param model
     * @param agentId
     * @return
     */
    @GetMapping("edit")
    public String editAgent(Model model, String agentId) {
        SysAgent agent = sysAgentService.queryAgentById(Long.valueOf(agentId));
        model.addAttribute("agent", agent);
        return "agent/agentDetail";
    }

    /**
     * 编辑代理商
     * @param agentForm
     * @return
     */
    @PostMapping("/edit")
    @ResponseBody
    public ResultEntity editAgent(SysAgent agentForm) {
        int num = sysAgentService.updateByAgentId(agentForm);
        if (num > 0) {
            return ResultUtli.DEFAULT_SUCCESS("编辑代理商成功", null);
        } else {
            return ResultUtli.DEFAULT_ERROR("编辑代理商失败");
        }
    }

    /**
     * 删除代理商
     * @param agentId
     * @return
     */
    @GetMapping("/delete")
    @ResponseBody
    public ResultEntity deleteAgent(String agentId) {
        List<String> agentIdsList = Arrays.asList(agentId);
        int num = sysAgentService.updateStatusBatch(agentIdsList, CommonConst.STATUS_INVALID);
        if (num > 0) {
            return ResultUtli.DEFAULT_SUCCESS("删除代理商成功", null);
        } else {
            return ResultUtli.DEFAULT_ERROR("删除代理商失败");
        }
    }

    /**分配商户列表查询*/
    @PostMapping("/assign/merch/list")
    @ResponseBody
    public Map<String,Object> assignMerchList(Long agentId){
        List<SysMerchant> merchList = sysMerchService.queryAll();
        Set<Integer> hasRoles = sysMerchService.findMerchByAgentId(agentId);
        Map<String, Object> jsonMap = new HashMap<>(2);
        jsonMap.put("rows", merchList);
        jsonMap.put("hasRoles",hasRoles);
        return jsonMap;
    }

    /**保存分配商户*/
    @PostMapping("/assign/merch")
    @ResponseBody
    public ResultEntity assignMerch(Long agentId, String merchIdStr){
        try {
            String[] merchIds = merchIdStr.split(",");
            List<String> merchIdsList = Arrays.asList(merchIds);
            sysAgentService.addAssignMerch(agentId,merchIdsList);
            List<String> agentIds = new ArrayList<>();
            agentIds.add(agentId.toString());

            return ResultUtli.DEFAULT_SUCCESS("分配角色成功", null);
        } catch (Exception e) {
            return ResultUtli.DEFAULT_ERROR("分配角色失败");
        }
    }

}
